﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Pixysoft.Framework.Remoting
{
    /// <summary>
    /// 用于手动配置映射 作为转移 包括了assembly转移、type转移
    /// </summary>
    public class RemotingConfiguration
    {
        List<RemotingAssemblyMapping> assemblyMappings = new List<RemotingAssemblyMapping>();
        
        public List<RemotingAssemblyMapping> AssemblyMappings
        {
            get { return assemblyMappings; }
            set { assemblyMappings = value; }
        }

        public RemotingAssemblyMapping SearchMapping(string assemblyName)
        {
            foreach (RemotingAssemblyMapping mapping in AssemblyMappings)
            {
                if (string.Equals(assemblyName, mapping.AssemblyName))
                    return mapping;
            }

            return null;
        }

        public RemotingAssemblyMapping SearchMapping(string assemblyName, string typeName)
        {
            foreach (RemotingAssemblyMapping mapping in AssemblyMappings)
            {
                if (!string.Equals(assemblyName, mapping.AssemblyName, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!string.Equals(typeName, mapping.TypeName, StringComparison.OrdinalIgnoreCase))
                    continue;

                return mapping;
            }

            return null;
        }
    }

    public class RemotingAssemblyMapping
    {
        string assemblyName;
        string mappingAssemblyName;

        string typeName;
        string mappingTypeName;

        public RemotingAssemblyMapping()
        {
        }
        public RemotingAssemblyMapping(string assemblyName, string mappingName)
        {
            this.assemblyName = assemblyName;
            this.mappingAssemblyName = mappingName;
        }
        public RemotingAssemblyMapping(string assemblyName, string mappingName,string typeName,string mappingTypeName)
        {
            this.assemblyName = assemblyName;
            this.mappingAssemblyName = mappingName;
            this.typeName = typeName;
            this.mappingTypeName = mappingTypeName;
        }

        [XmlAttribute("name")]
        public string AssemblyName
        {
            get { return assemblyName; }
            set { assemblyName = value; }
        }
        [XmlAttribute("mapping")]
        public string MappingAssemblyName
        {
            get { return mappingAssemblyName; }
            set { mappingAssemblyName = value; }
        }

        [XmlAttribute("type")]
        public string TypeName
        {
            get { return typeName; }
            set { typeName = value; }
        }
        [XmlAttribute("mappingtype")]
        public string MappingTypeName
        {
            get { return mappingTypeName; }
            set { mappingTypeName = value; }
        }
    }
}

